package com.yjt.sousou.base;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.igexin.sdk.PushManager;
import com.igexin.sdk.PushService;
import com.yjt.sousou.R;
import com.yjt.sousou.push.ReceiveIntentService;
import com.yjt.sousou.utils.StatusBarCompat;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者：chs
 * 时间：2018-10-22 14:32
 * 描述：
 */
public abstract class BaseActivity extends AppCompatActivity {

    private Unbinder mUnbinder = null;
    protected int mScreenWidth;
    protected abstract Object setContentLayout();
    protected abstract void init( Bundle savedInstanceState);
    protected String mUserId;
    @Override
        protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        if (setContentLayout() instanceof Integer) {
            setContentView((Integer) setContentLayout());
        } else if (setContentLayout() instanceof View) {
           setContentView((View) setContentLayout());
        } else {
            throw new ClassCastException("type of setLayout() must be int or View!");
        }
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.colorPrimary));
//        StatusBarCompat.setLightMode(this);
        mUnbinder = ButterKnife.bind(this);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        mScreenWidth = metrics.widthPixels;
        PushManager.getInstance().initialize(this.getApplicationContext(), PushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), ReceiveIntentService.class);
        init(savedInstanceState);
    }
    protected void startActivity(Class clazz){
        Intent intent = new Intent(this,clazz);
        startActivity(intent);
    }
    protected void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
    public static boolean isAPPBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            return topActivity.getClassName().equals(context.getClass().getName());
        }
        return false;
    }
    /**
     * 让屏幕变暗
     */
    public void makeWindowDark() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 0.5f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);
    }

    /**
     * 让屏幕变亮
     */
    public void makeWindowLight() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = 1f;
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);
    }

    //设置按钮样式
    protected void setState(List<TextView> textViews , int position){
        for (int i = 0; i < textViews.size(); i++) {
            if(position == i){
                textViews.get(i).setBackgroundResource(R.drawable.login_btn_bg);
                textViews.get(i).setTextColor(ContextCompat.getColor(this,R.color.white));
            }else {
                textViews.get(i).setBackgroundResource(R.drawable.work_state_out_bg);
                textViews.get(i).setTextColor(ContextCompat.getColor(this,R.color.gray_text));
            }
        }
    }

    /**
     * @param view 标题栏左侧按钮点击事件
     */
    public void goBack(View view) {
        onBackPressed();
    }
}
